//
// Created by THE13 on 2024/2/14.
//

#include "driver_interface.h"

/*0.---------------------------------------- State Private function  ---------------------------------------- */
static void sdl2_driver_init(void);

_SDL2_t SDL2 = {
	/*0.init*/
	 SDL2_init,
	/*1.set pencil mode*/
	 SDL2_setPencilMode,
	/*2.draw string*/
	 SDL2_setFont,
    /*3.draw float*/
	 SDL2_drawFloat,
    /*4.draw string*/
	 SDL2_drawString,
	 SDL2_drawCN,
	/*5.draw box*/
	 SDL2_drawBox,
    /*5.draw box and boundary*/
   SDL2_drawBoxBg,
	/*6.draw Rbox*/
	 SDL2_drawRBox,
	/*7.draw frame*/
	 SDL2_drawFrame,
	/*8.draw Rframe*/
	 SDL2_drawRFrame,
	/*9.draw h line*/
	 SDL2_drawHLine,
	/*10.draw v line*/
	 SDL2_drawVLine,
	/*11.draw  line*/
	 SDL2_drawLine,
	/*12.draw  circle*/
	 SDL2_drawCircle,
	/*13.draw  disc*/
	 SDL2_drawDisc,
	/*14.draw  image*/
	 SDL2_drawImage,
    /*15.set  pixel*/
	 SDL2_setPixel,
	/*end.send buff*/
	 SDL2_sendBuff,
	/*end.clean buff*/
	 SDL2_cleanBuff,
};


/*1.---------------------------------------- Public function  ---------------------------------------- */
void SDL2_init(void)
{
	ST7789_Init();
	
	sdl2_driver_init();
}

void SDL2_setPencilMode(uint8_t mode ,int drawColor,int bgColor)
{
	/*1.���û滭ģʽ*/
	sdl2_setMode(&sdl2,mode);
	/*2.���û�����ɫ*/
	sdl2_setPencil(&sdl2,drawColor,bgColor);
}

void SDL2_setFont(FontDefv2 font)
{
	sdl2_set_font(&sdl2, font);
}

/*2. ---------------------------------------- Draw function  ---------------------------------------- */

void SDL2_drawFloat(int16_t x, int16_t y, float num)
{
	sdl2_draw_float(&sdl2, x,  y,  num);
}

/*2.draw string*/
void SDL2_drawString(int16_t x, int16_t y, const char *str)
{
		sdl2_draw_string(&sdl2,x,y,str);
}

void SDL2_drawCN(int16_t x, int16_t y,FontDefv2 font)
{
		sdl2_draw_cn(&sdl2,x,y,font);
}
/*3.draw box*/
void SDL2_drawBox(int16_t x, int16_t y, uint16_t w, uint16_t h)
{
		sdl2_DrawBox(&sdl2, x, y,  w,  h);
}

void SDL2_drawBoxBg(int16_t x, int16_t y, uint16_t w, uint16_t h)
{
		/*1.copy temp value*/
		uint16_t draw_color = sdl2.draw_color;
		uint8_t mode = sdl2.mode;
		/*2.draw the box*/
		sdl2_DrawBox(&sdl2, x, y,  w,  h);
		/*3.set the pencil mode in normal*/
		sdl2.draw_color = sdl2.bg_color;
		sdl2.mode = NORMAL;
		sdl2_DrawFrame(&sdl2, x, y,  w,  h);
		sdl2_DrawFrame(&sdl2, x-1, y-1,  w+2,  h+2);
		/*4.resume temp value*/
	  sdl2.draw_color = draw_color;
		sdl2.mode = mode;
}
/*4.draw Rbox*/
void SDL2_drawRBox(int16_t x, int16_t y, uint16_t w, uint16_t h,uint16_t r)
{
		sdl2_DrawRBox(&sdl2, x, y,  w,  h,r);
}
/*5.draw frame*/
void SDL2_drawFrame(int16_t x, int16_t y, uint16_t w, uint16_t h)
{
		sdl2_DrawFrame(&sdl2, x, y,  w,  h);
}
/*6.draw Rframe*/
void SDL2_drawRFrame(int16_t x, int16_t y, uint16_t w, uint16_t h,uint16_t r)
{
		sdl2_DrawRFrame(&sdl2, x, y,  w,  h,r);
}
/*7.draw h line*/
void SDL2_drawHLine(int16_t x, int16_t y, uint16_t len)
{
		sdl2_DrawHLine(&sdl2, x, y,  len);
}
/*6.draw v line*/
void SDL2_drawVLine(int16_t x, int16_t y, uint16_t len)
{
		sdl2_DrawVLine(&sdl2, x, y,  len);
}
/*7.draw  line*/
void SDL2_drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1)
{
		draw_line(&sdl2, x0, y0,  x1,y1);
}
/*8.draw  circle*/
void SDL2_drawCircle(int16_t x, int16_t y, uint16_t rad, uint8_t option)
{
		sdl2_DrawCircle(&sdl2,x,y,rad,option);
}
/*9.draw  disc*/
void SDL2_drawDisc(int16_t x, int16_t y, uint16_t rad, uint8_t option)
{
		sdl2_DrawDisc(&sdl2,x,y,rad,option);
}

/*10.draw  image*/
void SDL2_drawImage(int16_t x, int16_t y, int16_t w, int16_t h, const uint16_t *data)
{
		sdl2_DrawImage( x,  y,  w,  h,data);
}

/*11.set  pixel*/
void SDL2_setPixel(int16_t x, int16_t y)
{
		sdl2_setPixBuffer(&sdl2,x,y);
}

/*end.send buff*/
void SDL2_sendBuff(void)
{
	sdl2_sendbuffer();
}

/*end.send buff*/
void SDL2_cleanBuff(void)
{
	int i, j;

    for (i = 0; i < SCREEN_HEIGHT; i++) {
        for (j = 0; j < SCREEN_WIDTH; j++) {
					disp_buf[i][j] = WHITE;
//					sdl2_setMode(&sdl2,NORMAL);
//					sdl2_setPencil(&sdl2,WHITE,WHITE);
//					sdl2_setPixBuffer(&sdl2,i,j);
        }
    }
}


/*0. ---------------------------------------- Private function  ---------------------------------------- */
static void sdl2_driver_init(void)//init the driver
{
		/*1.����BUFFERȫΪ��ɫ*/
		sdl2_cleanBuffer();
		/*2.���û滭ģʽ*/
		sdl2_setMode(&sdl2,NORMAL);
		/*3.���û�����ɫ*/
		sdl2_setPencil(&sdl2,BLACK,WHITE);
	
		/*4.���û滭ģʽ*/
		sdl2_setMode(&sdl2,NORMAL);
		/*5.���û�����ɫ*/
		sdl2_setPencil(&sdl2,0x0000,0x0000);
}


